﻿using Namer.Pages;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Namer
{
    public static class UI
    {
        public static UIForm MainForm = new UIForm()
        {
            Size = new Size(500, 350),
            Text = "点名器R",
            MaximizeBox = false,
            MinimizeBox = false,
            Style = UIStyle.Colorful
        };

        private static HomePage home = new HomePage();
        private static RandomNamePage rna = new RandomNamePage();
        private static StopNamePAge stp = new StopNamePAge();
        private static SettingPage sep = new SettingPage();

        private static int oldslect = 0;
        private static UIPanel p1 = new UIPanel() { Dock = DockStyle.Fill };
        public static void InterlizeUI(string[] args)
        {
            UITabControlMenu nv1 = new UITabControlMenu()
            {
                Dock = DockStyle.Left,
                Width = 50,
                TextAlignment = HorizontalAlignment.Left,
                Font = new Font("Segoe MDL2 Assets",15)
            };MainForm.Controls.Add(nv1);
            nv1.TabPages.Add("\ue700");
            nv1.TabPages.Add("\ue716");
            nv1.TabPages.Add("\ue81a");
            nv1.TabPages.Add("\ue713");
            
            BackGround.OnDone(0);
            nv1.Selected += Nv1_Selected;

            

            MainForm.Controls.Add(p1);
            p1.Width = MainForm.Width - nv1.Width;
            

            MainForm.Show();
            nv1.SelectedIndex = 1;
            MainForm.FormClosed += MainForm_FormClosed;
            MainForm.Load += MainForm_Load;

            AppDomain.CurrentDomain.AssemblyResolve -= Program.CurrentDomain_AssemblyResolve;
            Assembly asm = Assembly.GetExecutingAssembly();
            ResourceManager rm = new ResourceManager("Namer.Properties.Resources", asm);
            Icon img = rm.GetObject("ico") as Icon;

            MainForm.Icon = img;
        }

        private static void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private static bool isOpen = false;
        private static void Nv1_Selected(object sender, TabControlEventArgs e)
        {
            UITabControlMenu dataurm = (UITabControlMenu)sender;
            switch (dataurm.SelectedIndex)
            {
                case 0:
                    ChangeManuState(dataurm);
                    break;
                case 1:
                    ChangeToPage(1, dataurm.Width);
                    break;
                case 2:
                    ChangeToPage(2, dataurm.Width);
                    break;
                case 3:
                    ChangeToPage(3, dataurm.Width);
                    break;
            };
            oldslect = dataurm.SelectedIndex;
        }

        public static void ChangeToPage(int Pageid, int x)
        {
            p1.Controls.Clear();
            switch (Pageid)
            {
                case 0:
                    foreach (Control ctr in home.GetControls(x)) p1.Controls.Add(ctr);
                    break;
                case 1:
                    foreach (Control ctr in rna.GetControls(x)) p1.Controls.Add(ctr);
                    break;
                case 2:
                    foreach (Control ctr in stp.GetControls(x)) p1.Controls.Add(ctr);
                    break;
                case 3:
                    foreach (Control ctr in sep.GetControls(x)) p1.Controls.Add(ctr);
                    break;
            };
        }

        private static void ChangeManuState(UITabControlMenu dataurm)
        {
            if (isOpen)
            {
                isOpen = false;
                dataurm.Width = 50;
                for (int i = 0; i <= dataurm.TabPages.Count - 1; i++)
                {
                    switch (i)
                    {
                        case 0:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "\ue700");
                            break;
                        case 1:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "\ue716");
                            break;
                        case 2:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "\ue81a");
                            break;
                        case 3:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "\ue713");
                            break;
                    }
                    dataurm.Font = new Font("Segoe MDL2 Assets", 15);
                    dataurm.SelectedIndex = oldslect;
                }
                GC.Collect();
            }
            else
            {
                isOpen = true;
                dataurm.Width = 150;
                for (int i = 0; i <= dataurm.TabPages.Count - 1; i++)
                {
                    switch (i)
                    {
                        case 0:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "关闭导航");
                            break;
                        case 1:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "随机点名");
                            break;
                        case 2:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "暂停点名");
                            break;
                        case 3:
                            dataurm.TabPages.Remove(dataurm.TabPages[i]);
                            dataurm.TabPages.Insert(i, "设置");
                            break;
                    }
                    dataurm.Font = new Font("Segoe UI Light", 13);
                    dataurm.SelectedIndex = oldslect;
                }
                GC.Collect();
            }
        }

        private static void MainForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
